package ro.peoplecentric.ctc.faces;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 * JSF empty string to null converter.
 *
 * @author pguzun
 */
@FacesConverter("emptyStringToNullConverter")
public class EmptyStringToNullConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return (value == null || value.isEmpty()) ? null : value;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return (value == null) ? null : String.valueOf(value);
    }
}
